import re,os,sys
import time,datetime
import base64
import binascii
import numpy as np
import pandas as pd

def handle_time_remind(param):

	clock1=param['clock1']
	clock2=param['clock2']
	clock3=param['clock3']
	clock1_sw=param['clock1_sw']
	clock2_sw=param['clock2_sw']
	clock3_sw=param['clock3_sw']
	clock_type={'take_pill':'1','long_sit_alarm':'3','drink_water':'2','clock':'2'}
	

	def get_clock_rate(rate,clock,customize):
		if rate=='alarm_everyday':
			return '1234567'
		elif rate=='alarm_one':
			day = datetime.datetime.now().isoweekday()
			now_hour = datetime.datetime.now().hour
			now_minute = datetime.datetime.now().minute
			clock=clock.split(':')
			rece_hour=clock[0]
			rece_minute=clock[1]
			if int(rece_hour)<now_hour:
				day=day+1
			elif int(rece_hour)==now_hour:
				if int(rece_minute)<now_minute:
					day=day+1
			if day>7:
				day=1
			return str(day)
		elif rate=='customize':
			customize=customize.split(',')
			customize=''.join(customize)
			return customize

	clock1_rate=get_clock_rate(param['clock1_rate'],clock1,param['customize1'])
	clock2_rate=get_clock_rate(param['clock2_rate'],clock2,param['customize2'])
	clock3_rate=get_clock_rate(param['clock3_rate'],clock3,param['customize3'])
	colck1_param=''.join(clock1.split(':'))+','+clock1_rate+','+clock1_sw+','+clock_type[param['clock1_type']]
	colck2_param=''.join(clock2.split(':'))+','+clock2_rate+','+clock2_sw+','+clock_type[param['clock2_type']]
	colck3_param=''.join(clock3.split(':'))+','+clock3_rate+','+clock3_sw+','+clock_type[param['clock3_type']]
	res_param='1,3,'+colck1_param+'@'+colck2_param+'@'+colck3_param
	return res_param

def jiai_command(device_id,kind,param,model,sender='admin'):
	print(device_id,kind,param,model)
	command=''
	title_dict={'send_rate':'IWBP33','health_rate':'IWBP86',
				'sos1':'IWBP12','sos2':'IWBP12','sos3':'IWBP12','sos':'IWBP12',
				'step_cunt_btn':'IWBP21','time_remind':'IWBP85','work_model':'IWBP33'}

	title_list={'call_now':'IWBP16','factory_set':'IWBP17','dev_reset':'IWBP18','turn_off_dev':'IWBP31',
				'heart_rate_detect':'IWBPXL','blood_pres_detect':'IWBPXY','health_detect':'IWBPXY'}

	dialog_talk={'voice':'IWBP28'}
	data_len=''
	if kind in title_dict:
		if kind=='sos':
			sos1=param['sos1']
			sos2=param['sos2']
			sos3=param['sos3']
			if param['sos1_sw']!='on':
				sos1=''
			if param['sos2_sw']!='on':
				sos2=''
			if param['sos3_sw']!='on':
				sos3=''
			param=sos1+','+sos2+','+sos3
		elif kind=='time_remind':
			param=handle_time_remind(param)

		elif  kind=='send_rate':
			param='8,'+param+',1'
		elif kind=='health_rate':
			param='2,'+str(int(int(param)/60))

		kind=title_dict[kind]
		command=kind.encode()+b','+device_id.encode()+b',080835,'+param.encode()+b'#'

	elif kind in title_list:
		kind=title_list[kind]
		command=kind.encode()+b','+device_id.encode()+b',080835#'
		
	elif kind in dialog_talk:
		if kind=='voice':
			print(param)
			handle_param=''
			for x in range(len(sender)):
				first=hex(ord(sender[x]))[2:]
				handle_param+=first
			sender_name=handle_param.encode('gbk')
			# print('param=============>',param)
			param=param.split(',')[1]
			param=base64.b64decode(param)
			param=binascii.hexlify(param).decode()
			textArr = re.findall('.{2048}', param)
			textArr.append(param[(len(textArr)*2048):])
			cut_result=pd.DataFrame(textArr).to_dict()[0]
			first_msg=bytes.fromhex(cut_result[0])
			kind=dialog_talk[kind]
			
			# print(cut_result)
			command={'command':kind.encode()+b','+sender_name+b',080835,'+str(len(cut_result)).encode()+b',1,1024'+b','+first_msg+b'#',
					 'voice_dict':cut_result}

			# print(command)

	return command


# print(jiai_command('350182825342289','health_rate','60','0'))